﻿#region

using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using HttpHandlerSpace;

#endregion

namespace JetLoginTool.Engine
{
	/// <summary>
	/// Handles update checks and current version number\update url
	/// </summary>
	public static class JetEngineVersion
	{
		#region Constants
		private static readonly Uri VERSION_URL = new Uri("http://jetlogintool.codeplex.com/releases/");
		private static readonly Regex versionRegexChecker = new Regex(@"<span[ a-z=""]*id=""TitleLabel""[ a-z=""]*>.*? - ([0-9]\.[0-9]{1,2}\.[0-9]{4}\.[0-9]{3,5}).*?</span>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
		#endregion
		#region Methods
		public static string CheckForNewVersion()
		{
			string newVersion = null;

			HttpHandler tempHandler = new HttpHandler();

			try
			{
				String myResponse = HttpHandler.responseToString(tempHandler.getResponse(HttpHandler.makeGetRequest(VERSION_URL)));
				Match versionMatcher = versionRegexChecker.Match(myResponse);

				if (versionMatcher.Success)
				{
					newVersion = versionMatcher.Groups[1].ToString();
				}

				myResponse = null;
			}
			catch (SystemException)
			{ }

			versionRegexChecker.Match(string.Empty);

			return newVersion;
		}
		#endregion
	}
}
